<?php
namespace Home\Controller;
use Think\Controller;

class UserController extends Controller{
	
	//
	public function user_page(){
		
		$user_id = session("user_id");
		session('need_url',__MODULE__."/User/user_page");
		if (isset($user_id)) {
			$user = D('User');
			$info = $user -> find($user_id);

			$info['user_photo'] == '' && $info['user_photo'] = 'Public/images/Edit_normal_state.png';

			$this -> assign('info',$info);
			$this -> display();
		}else{

			$this -> redirect('User/login'); 
		}	
	}


	public function update(){

		$arr['id'] = session('user_id');
		$arr['user_tel'] = $_POST['user_tel'];
		$arr['user_addr'] = $_POST['user_addr'];
		$arr['user_name'] = $_POST['user_name'];
		$arr['user_photo'] = $_POST['user_photo'];
		$user = D('User');

		$re = $user -> save($arr);
        
        if ($re) {
        	echo session('need_url');
        }else{
        	echo "0";
        }
	}

	public function personal_data(){

		$user_id = session('user_id');

		if ($user_id) {//登录过

			$user = D('User');
			$info = $user -> find($user_id);
			$info['user_photo'] == '' && $info['user_photo'] = 'Public/images/Edit_normal_state.png';
			
		}else{

			$this -> redirect('User/login');
		}

		$this -> assign('info',$info);
		$this -> display();
	}

	public function verify(){

		if ($_POST['phone']) {
			$num_v = $this -> generate_code();
			$phone = $_POST['phone'];
			$time = date('ymdhis',time());

			$verify = D('Verify');

			$arr = array(
				"tel" => $phone,
				"verify" => $num_v,
				"time" => $time
				);

			if($verify->where(['tel'=>$phone])->find()){
				$verify ->where(['tel'=>$phone])-> save($arr);
			}else{
				$verify -> add($arr);
			}

			$content = "欢迎使用E 智造科技信息平台, 您的验证码为 (" .$num_v.") ";

			$this->for_sms($phone,$content);
		}
	}

	protected function for_sms($phone_no, $content){
		$sn = 'SDK-BBX-010-15319';
		$password = 'b5201+8+';
		$params = '';
		$line = '';
		$flag = 0;
		$argv = array(
			'sn' => $sn,
			'pwd' => strtoupper(md5($sn . $password)),
			'mobile' => $phone_no,
			'content' => $content . '[奇步互动]',//iconv( "UTF-8", "gb2312//IGNORE" ,'您好测试短信[奇步互动]'),
			'ext' => '',
			'stime' => '',
			'msgfmt' => '',
			'rrid' => ''
		);
		foreach ($argv as $key => $value) {
			if ($flag != 0) {
				$params .= "&";
				$flag = 1;
			}
			$params .= $key . "=";
			$params .= urlencode($value);
			$flag = 1;
		}
		$length = strlen($params);
		$fp = fsockopen("sdk.entinfo.cn", 8061, $errno, $errstr, 10) or exit($errstr . "--->" . $errno);
		$header = "POST /webservice.asmx/mdsmssend HTTP/1.1\r\n";
		$header .= "Host:sdk.entinfo.cn\r\n";
		$header .= "Content-Type: application/x-www-form-urlencoded\r\n";
		$header .= "Content-Length: " . $length . "\r\n";
		$header .= "Connection: Close\r\n\r\n";
		$header .= $params . "\r\n";
		fputs($fp, $header);
		$inheader = 1;
		while (!feof($fp)) {
			$line = fgets($fp, 1024);
			if ($inheader && ($line == "\n" || $line == "\r\n")) {
				$inheader = 0;
			}
			if ($inheader == 0) {
			}
		}
		$line = str_replace("<string xmlns=\"http://tempuri.org/\">", "", $line);
		$line = str_replace("</string>", "", $line);
		$result = explode("-", $line);
		if (count($result) > 1)
			return false;
		else
			return true;
	}

	//登录页面
	public function login(){	
		
		$this -> display();		
	}

	//退出登陆
	public function logout(){

		//清除已登录用户
		session(NULL);
		cookie('user_id',NULL);
		
		session('need_url',__MODULE__."/Index/index");
		
		$this -> redirect('User/login'); 		
	}

	public function checkVerify(){
			
			$arr['tel'] = $_POST['phone'];
			$arr['verify'] = $_POST['num_v'];
				
			$verify = D('Verify');

			$re = $verify -> where($arr) ->find();

			if($re){
				$user = D('User');

				$arrU['user_tel'] = $_POST['phone'];
				$reU = $user -> where($arrU) -> find();

				if ($reU == NULL) {//未登录过

					$re = $user -> add($arrU);
					//设置cookie
					cookie('user_id',$re,3600*24*365);
					session('user_tel',$_POST['phone']);
					echo "1";

				}else{//登录过
					
					$user_id = $reU['id'];
					//设置cookie
					cookie('user_id',$user_id,3600*24*365);
					//设置session
					session('user_id',$user_id);

					echo session('need_url');
				}


				//去除验证码表的信息
				$id = $re['id'];
				$verify -> delete($id);
			}else{
				echo "0";
			}
	}


	function generate_code($length = 4) {
	    $min = pow(10 , ($length - 1));
	    $max = pow(10, $length) - 1;
	    return rand($min, $max);
	}


}